<?php
class ScansController extends AppController {

	var $name = 'Scans';
	var $helpers = array('Html', 'Form');
	
	function index () {
		// просто выводим форму ввода csv файла 	
	}
		
	function add () {
			if (!empty($this->data)) {
			$this->loadModel("Abiturient");
			/*
			 * читаем фото если оно есть в переменную для сохранения в таблице 
			 */
			unset($photofile);
			$photofile ='';
			if ($this->data["Scan"]["photo"]["tmp_name"]) {
				$f = fopen($this->data["Scan"]["photo"]["tmp_name"],"rb");
				$photofile = fread($f, $this->data['Scan']['photo']['size']);
				fclose($f);							
			}
			// открываем файл
			$csvfile = fopen($this->data["Scan"]["csvfile"]["tmp_name"],"r");
			//читаем первую строку там заголовки
			$line = fgetcsv($csvfile,1000, ";");
			while (!(feof($csvfile))){
				$passport = $line;
				// читаем последний раз там пусто
				$line= fgetcsv($csvfile,1000, ";");
			}
			fclose($csvfile);
			// устанавлиаем пол по паспорту
			if (iconv('Windows-1251', 'UTF-8', $passport[6]) == "МУЖ"){
				$gender_id = 2;
			}
			else {
				$gender_id = 1;	
			}
			// формируем массив из даты
			$birthday = explode ('.', $passport[5]);
			// формируем массив Abiturient для занесения записи в таблицу
			$newdata = array('Abiturient' => array(
									'lastname' => iconv('Windows-1251', 'UTF-8',  $passport[2]), 
									'firstname' => iconv('Windows-1251', 'UTF-8', $passport[3]),
									'surname' => iconv('Windows-1251', 'UTF-8', $passport[4]),  
									'photo' => $photofile, 
									'gender_id' => $gender_id, 
									'birthdate' => array('day' => $birthday[0], 'month' => $birthday[1], 'year' => $birthday[2]), 
									'citizenship_id' => '', 
									'passport_ser' => iconv('Windows-1251', 'UTF-8', $passport[8]), 
									'passport_num' => iconv('Windows-1251', 'UTF-8', $passport[9]), 
									'passport_data' => iconv('Windows-1251', 'UTF-8', $passport[11]." | ".$passport[13]), 
									'passport_birthplace' => iconv('Windows-1251', 'UTF-8', $passport[10]),
									'region_id' => '',
									'adress' => '',
									'schoolflanguage_id' => '', 
									'schoolgraduated ' => '',
									'educationdoctype_id' => '', 
									'originaleducationdoc' => '', 
									'educationdocdata' => '', 
									'phone' => '', 
									'mobile' => '', 
									'email' => '', 
									'inn' => '', 
									'insurance' => '', 
									'pension_certificate' => '', 
									'inoculations' => '', 
									'moreinfo' => '',
									'prepared' => 0, 
									'approved' => 0, 
									'enrolled' => 0,
									'canceled' => 0));
			
			//debug($newdata);
			$passport_ser = iconv('Windows-1251', 'UTF-8', $passport[8]);
			$passport_num = iconv('Windows-1251', 'UTF-8', $passport[9]);
			$firstname = iconv('Windows-1251', 'UTF-8', $passport[3]);
			$lastname = iconv('Windows-1251', 'UTF-8',  $passport[2]);
			$surname = iconv('Windows-1251', 'UTF-8', $passport[4]);
			$matches_count = $this->Abiturient->find('count', array('conditions' => array ('Abiturient.lastname' => $lastname, 'Abiturient.firstname' => $firstname, 'Abiturient.surname' => $surname, 'Abiturient.passport_ser' => $passport_ser, 'Abiturient.passport_num' => $passport_num)));
			if (!$matches_count) {
			$this->Abiturient->save($newdata);
			}
			$this->set('matches_count', $matches_count);
			$this->set('newdata', $newdata);		
			//$this->redirect(array('action' => 'index'));		
		}
	}
} 
?>